/*
 * MCU event manipulations header file
 *
 * Copyright (C) 2020 Long-Horn.
 *
 */
#ifndef MCU_EVENT_H
#define MCU_EVENT_H

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include "./serial/serial_io.h"
#include "./fifo/fifo_io.h"
#include "../event/event_queue.h"
#include "../event/event.h"

#ifdef __cplusplus
extern "C"  {
#endif

#define IO_BUF_LEN 128

typedef  struct _mcu_event_proxy
{
    // interface functions
    int (* start)(void *mcu_event_proxy);
    int (* stop)(void *mcu_event_proxy);
    int (* send_event)(void *mcu_event_proxy, event_s *event);
    int (* send_event_with_response)(void *mcu_event_proxy, event_s *event);
    
    int io_type; // 0 fifo_io ; 1 serial_io
    int timeout; // default 2seconds
    serial_io_s *serial_io;
    fifo_io_s *fifo_io;
    
    // event queue instance
    event_queue_s *event_queue;

    int running; // 1 indicate running, 0 need to exit

    // synchronization primitives
    pthread_mutex_t mutex;
    pthread_cond_t startable;

} mcu_event_proxy_s;

int create_mcu_event_proxy(mcu_event_proxy_s **mcu_event_proxy, event_queue_s *event_queue, int io_type, int timeout);
void destroy_mcu_event_proxy(mcu_event_proxy_s *mcu_event_proxy);

#ifdef __cplusplus
}
#endif

#endif	/* MCU_EVENT_H */
